Imports System.ComponentModel
Imports System.Web.UI

<ToolboxData("<{0}:ProviderPart runat=server></{0}:ProviderPart>")> _
    Public Class ProviderPart
    Inherits System.Web.UI.WebControls.WebParts.WebPart
    Implements IBookInfo
    Implements WebControls.WebParts.IWebPartField
    Implements WebControls.WebParts.IWebPartParameters

    Private myData As String = "ISBN Provider: 0130230731"
    Private strBookTitle As String = "Custom Controls and Web Parts"
    Private strBookAuthor As String = "Peter Vogel"
    Dim pcSchema As PropertyDescriptorCollection

    Property BookIdentifier() As String Implements IBookInfo.ISBN

        Get
            Return myData
        End Get

        Set(ByVal Value As String)
            myData = Value
        End Set

    End Property

    Public ReadOnly Property BookData() As String
        Get
            Return strBookTitle
        End Get
    End Property

    Public Property BookAuthor() As String
        Get
            Return strBookAuthor
        End Get
        Set(ByVal value As String)
            strBookAuthor = value
        End Set
    End Property

    Public Property BookTitle() As String
        Get
            Return strBookTitle
        End Get
        Set(ByVal value As String)
            strBookTitle = value
        End Set
    End Property

    Sub UpdateCache(ByVal CacheFlag As Boolean) Implements IBookInfo.UpdateCache
        '...business logic
    End Sub

    Function SortData(ByVal Direction As String) As Boolean Implements IBookInfo.SortData
        '...business logic
    End Function

    <WebControls.WebParts.ConnectionProvider("Provides ISBN", "ISBNConnection")> _
    Public Function IBookInfoProvider() As IBookInfo
        Return Me

    End Function

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        writer.Write("<b>Providing Data: " & myData & "</b>")
    End Sub

    Private Sub ProviderPart_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.Title = "Provider"
    End Sub

    Sub FieldValue(ByVal fld As WebControls.WebParts.FieldCallback) _
                        Implements WebControls.WebParts.IWebPartField.GetFieldValue

        fld.Invoke(Me.BookData)

    End Sub

    ReadOnly Property DataDescription() As ComponentModel.PropertyDescriptor _
                              Implements WebControls.WebParts.IWebPartField.Schema
        Get
            Dim pdc As PropertyDescriptorCollection

            pdc = TypeDescriptor.GetProperties(Me)
            Return pdc("BookData")
        End Get

    End Property

    <WebControls.WebParts.ConnectionProvider("Provides IWebPartField")> _
    Public Function IWebPartFieldProvider() As WebControls.WebParts.IWebPartField

        Return Me

    End Function

    
    Sub sc(ByVal schema As PropertyDescriptorCollection) _
      Implements WebControls.WebParts.IWebPartParameters.SetConsumerSchema

        pcSchema = schema

    End Sub

    Public ReadOnly Property IParmsSchema() As System.ComponentModel.PropertyDescriptorCollection _
                    Implements System.Web.UI.WebControls.WebParts.IWebPartParameters.Schema
        Get
            Dim pdcMe As PropertyDescriptorCollection
            Dim pdcReturn(pcSchema.Count) As PropertyDescriptor
            Dim ing As Integer

            pdcMe = TypeDescriptor.GetProperties(Me)
            For Each pd As PropertyDescriptor In pcSchema
                If pdcMe(pd.Name) IsNot Nothing Then
                    ing += 1
                    pdcReturn(ing) = pd
                End If
            Next

            Return New PropertyDescriptorCollection(pdcReturn)
        End Get

    End Property

    Sub gpd(ByVal prm As WebControls.WebParts.ParametersCallback) _
        Implements WebControls.WebParts.IWebPartParameters.GetParametersData
        Dim dict As New StateBag
        Dim prop As PropertyDescriptor

        For Each prop In pcSchema
            Select Case prop.Name
                Case "BookTitle"
                    dict.Add("BookTitle", Me.BookTitle)
                Case "BookAuthor"
                    dict.Add("BookAuthor", Me.BookAuthor)
            End Select
        Next

        prm.Invoke(dict)

    End Sub

End Class
